﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using Camera;

namespace DebugLibrary
{
    
    public class DrawApi2D
    {

        GraphicsDevice device = null;
        CameraService cameraService = null;

        public void Initialize(GraphicsDevice device, CameraService cameraService)
        {
            this.device = device;
            this.cameraService = cameraService;
        }

        public void DrawLine(float x1, float y1, float x2, float y2)
        {
            this.DrawLine(new Vector2(x1, y1), new Vector2(x2, y2));
        }

        public void DrawLine(float x1, float y1, float x2, float y2, Color color)
        {
            this.DrawLine(new Vector2(x1, y1), new Vector2(x2, y2), color);
        }

        public void DrawLine(Vector2 start, Vector2 end)
        {

            this.DrawLine(start, end, Color.White);

        }

        public void DrawLine(Vector2 start, Vector2 end, Color color)
        {

            var vertices = new[] {new VertexPositionColor(new Vector3(start.X, 0, start.Y), color), new VertexPositionColor(new Vector3(end.X, 0, end.Y), color) };
            device.DrawUserPrimitives(PrimitiveType.LineList, vertices, 0, 1);

        }

        public void DrawCircle(float x1, float y1, double radius)
        {
            this.DrawCircle(new Vector2(x1, y1), radius);
        }

        public void DrawCircle(float x1, float y1, double radius, Color color)
        {
            this.DrawCircle(new Vector2(x1, y1), radius, color);
        }
   
        public void DrawCircle(Vector2 center, double radius)
        {
            this.DrawCircle(center, radius, Color.White);
        }

        public void DrawCircle(Vector2 center, double radius, Color color) 
        {
             
            const int CirclePoits = 36;
            double angle = MathHelper.TwoPi / CirclePoits;

            var pointList = new VertexPositionColor[CirclePoits + 1];

            for (int i = 0; i < CirclePoits; i++)
            {
                Vector3 point = new Vector3((float)(Math.Sin(angle * i) * radius + center.X), 0, (float)(Math.Cos(angle * i) * radius + center.Y));
                pointList[i] = new VertexPositionColor(point, color);
            }
            pointList[CirclePoits] = pointList[0];

            device.DrawUserPrimitives(PrimitiveType.LineStrip, pointList, 0, CirclePoits);

        }

        public void DrawPoint(float x, float y, Color color)
        {
            
            Vector3 pointPos = new Vector3(x, 0, y);

            var vertices = new[] { new VertexPositionColor(pointPos, color), new VertexPositionColor(GetEndPointForDot(pointPos), color) };

            device.DrawUserPrimitives(PrimitiveType.LineList, vertices, 0, 1);
        }

        private Vector3 GetEndPointForDot(Vector3 start)
        {
            // Convert start point to screen space
            Vector3 screenPoint = device.Viewport.Project(start, cameraService.getProjection(), cameraService.getView(), Matrix.Identity);

            // Screen space is defined in pixels so adding (1,0,0) moves it right one pixel
            screenPoint += Vector3.Right;

            // Finally unproject it back into world space
            return device.Viewport.Unproject(screenPoint, cameraService.getProjection(), cameraService.getView(), Matrix.Identity);
        }

    }
}
